﻿using System;
using System.Windows.Forms;
using Smart.Controls;

namespace Smart
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Closed(object sender, EventArgs e)
        {
            Program.Session.CloseSession();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            int sure = Cevir.Parse<int>(Program.Session[Settings.SyncTime]);
            if (sure > 0)
            {
                timer1.Interval = 1000 * sure;
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
            }

            int depono = Program.Session.VarsayilanDepo();
            if (depono > 0)
            {
                using (Query q = new Query(ProgramTipleri.SqlCe))
                {
                    q.Add(string.Format("SELECT     depo_adi  FROM         TBL_DEPO WHERE     (dep_kodu = {0})", depono));
                    lblMesaj.Text = string.Format("Kullanılan Depo={0}", q.ExecScaler<string>());
                }
            }

            using (Query q = new Query(ProgramTipleri.SqlCe))
            {
                DbMenu m = new DbMenu();
                foreach (string item in m.BulkImport(Cevir.Parse<int>(Program.Session[Settings.KullaniciId])))
                {

                }

                q.Add("select * from TBL_MENU");
                var sonuc = q.ToList(new { kod = 0, kull = true, adi = "", sifreli = true });
                sonuc = q.ToList(new { kod = 0, kull = true, adi = "", sifreli = true });


                foreach (var item in sonuc)
                {
                    if ((item.kod % 10) == 0)
                    {
                        LstMain.Items.Add(new ListItem()
                        {
                            MenuKod = item.kod,
                            Text = item.adi,
                            Sifreli = item.sifreli,
                            Kullanilabilir = item.kull,
                            ImageIndex = ImageIndex(item.kod)
                        });
                    }
                }
                LstMain.Items.Add(new ListItem()
                {
                    MenuKod = 0,
                    Text = "Çıkış",
                    Sifreli = false,
                    Kullanilabilir = true,
                    ImageIndex = 0
                });
            }

        }

        private int ImageIndex(int menuKod)
        {
            switch (menuKod)
            {
                case 10:
                    return 1;
                case 20:
                    return 2;
                case 30:
                    return 3;
                case 40:
                    return 4;
                case 50:
                    return 5;
                case 80:
                case 90:
                case 60:
                case 100:
                    return 6;
                case 70:
                    return 7;
                default:
                    return 0;
            }
        }

        private void LstMain_ItemActivate(object sender, EventArgs e)
        {
            if (LstMain.SelectedIndices.Count > -1)
            {
                int idx = LstMain.SelectedIndices[0];
                ListItem item = LstMain.Items[idx] as ListItem;
                if (item.Kullanilabilir)
                {
                    switch (item.MenuKod)
                    {
                        case 10://sayım
                            Cursor.Current = Cursors.WaitCursor;
                            frmSayim sayim = new frmSayim();
                            sayim.Show();
                            Cursor.Current = Cursors.Default;
                            break;
                        case 20://deposevk
                            frmBelgeGiris belge = new frmBelgeGiris();
                            belge.IslemTipi = frmBelgeGiris.IslemTipleri.DepoSevk;
                            belge.Show();
                            break;
                        case 30://irsaliye
                            frmBelgeGiris belge1 = new frmBelgeGiris();
                            belge1.IslemTipi = frmBelgeGiris.IslemTipleri.Irsaliye;
                            belge1.Show();
                            break;
                        case 40://sipariş
                            frmBelgeGiris belge2 = new frmBelgeGiris();
                            belge2.IslemTipi = frmBelgeGiris.IslemTipleri.Siparis;
                            belge2.Show();
                            break;
                        case 50://transfer
                            if (new frmInputBox().ShowDialog() == DialogResult.OK)
                            {
                                Cursor.Current = Cursors.WaitCursor;
                                frmTransfer trn = new frmTransfer();
                                trn.Show();
                                Cursor.Current = Cursors.Default;
                            }
                            break;
                        case 60: //evrak yönetimi
                            frmEvrakYonetimi yon = new frmEvrakYonetimi();
                            yon.Show();
                            break;
                        case 70: //müşteri açılış
                            frmCariListe lst = new frmCariListe();
                            lst.Show();
                            break;
                        case 80:
                            frmEvrYonetim yon2 = new frmEvrYonetim();
                            yon2.Vade = 0;
                            yon2.Show();
                            break;
                        case 90:
                            frmStokBilgi bilgi = new frmStokBilgi();
                            bilgi.Show();
                            break;
                        case 100:
                            frmEtiket etiket = new frmEtiket();
                            etiket.Show();
                            break;
                        case 0:
                            if (MessageBox.Show("Çıkmak istediğinize eminmisiniz ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                                this.Close();
                            break;
                    }
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                Synchronization.Get.StartSync();
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            timer1.Enabled = true;
        }
    }
}